﻿// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证（版本 2.0）进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动！任何基于本项目二次开发而产生的一切法律纠纷和责任，我们不承担任何责任！

namespace Admin.NET.Erp.FixedAssetSystem.Service;


/// <summary>
/// 固定资产服务 🧩
/// </summary>
[ApiDescriptionSettings(FixedAssetSystemConst.GroupName, Order = 460)]

public class FixedAssetService : IDynamicApiController, ITransient, IFixedAssetService
{
    private readonly UserManager _userManager;
    private readonly SysUserRoleService _sysUserRoleService;
    private readonly SqlSugarRepository<FixedAssetEntity> _fixedAssetRepository;

    public FixedAssetService(
        SqlSugarRepository<FixedAssetEntity> fixedAssetRepository,
        UserManager userManager,
        SysUserRoleService sysUserRoleService)
    {
        _fixedAssetRepository = fixedAssetRepository;
        _userManager = userManager;
        _sysUserRoleService = sysUserRoleService;
    }


    /// <summary>
    /// 获取固定资产分页列表 🔖
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "Page"), HttpPost]
    [DisplayName("获取固定资产分页列表")]
    public async Task<SqlSugarPagedList<FixedAssetOutput>> Page(PageFixedAssetInput input)
    {
        // 当前用户已拥有的角色集合
        var roleIdList = _userManager.SuperAdmin ? new List<long>() : await _sysUserRoleService.GetUserRoleIdList(_userManager.UserId);

        var _query = await _fixedAssetRepository.AsQueryable()
            .Includes(x => x.Category)
            .WhereIF(_userManager.SuperAdmin && input.TenantId > 0, u => u.TenantId == input.TenantId)
            .WhereIF(!_userManager.SuperAdmin, u => u.TenantId == _userManager.TenantId) // 若非超管，则只能操作本租户的角色
            .WhereIF(!_userManager.SuperAdmin && !_userManager.SysAdmin, u => u.CreateUserId == _userManager.UserId || roleIdList.Contains(u.Id)) // 若非超管且非系统管理员，则只能操作自己创建的角色|自己拥有的角色
            .WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name.Contains(input.Name))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Code), u => u.Code.Contains(input.Code))
            .OrderBy(u => new { u.OrderNo, u.Id })
            .ToPagedListAsync(input.Page, input.PageSize);


        return _query.Adapt<SqlSugarPagedList<FixedAssetOutput>>();
    }

    /// <summary>
    /// 增加固定资产 🔖
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "Add"), HttpPost]
    [DisplayName("增加固定资产")]
    public async Task AddFixedAsset(AddFixedAssetInput input)
    {
        await _fixedAssetRepository.InsertAsync(input.Adapt<FixedAssetEntity>());
    }

    /// <summary>
    /// 批量增加固定资产 🔖
    /// </summary>
    /// <param name="input"></param>
    /// <param name="num">批量添加数量</param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "BatchAdd"), HttpPost]
    [DisplayName("批量增加固定资产")]
    public async Task BatchAddFixedAsset(AddFixedAssetInput input, int num)
    {
        await _fixedAssetRepository.InsertAsync(input.Adapt<FixedAssetEntity>());
    }

    /// <summary>
    /// 获取固定资产列表 🔖
    /// </summary>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "List"), HttpGet]
    [DisplayName("获取固定资产列表")]
    public async Task<List<FixedAssetOutput>> GetList()
    {
        return null;
    }

    /// <summary>
    /// 更新固定资产 🔖
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "Update"), HttpPost]
    [DisplayName("更新固定资产")]
    public async Task UpdateFixedAsset(UpdateFixedAssetInput input)
    {
        await _fixedAssetRepository.UpdateAsync(input.Adapt<FixedAssetEntity>());
    }

    /// <summary>
    /// 删除固定资产 🔖
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "Delete"), HttpPost]
    [DisplayName("删除固定资产")]
    public async Task DeleteFixedAsset()
    {
    }

    /// <summary>
    /// 假删除固定资产 🔖
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "FakeDelete"), HttpPost]
    [DisplayName("假删除固定资产")]
    public async Task FakeDeleteFixedAsset(long id)
    {

    }

    /// <summary>
    /// 固定资产绑定类别 🔖
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "DistributeToCategory"), HttpPost]
    [DisplayName("固定资产绑定类别")]
    public async Task FixedAssetDistributeToCategory(BindFixedAssetToCategoryInput input)
    {
        //var _isSuccess = await _fixedAssetRepository.UpdateSetColumnsTrueAsync(
        //     columns: s => s.CategoryId == input.CategoryId,
        //     whereExpression: s => s.Id == input.FixedAssetId);
    }

    /// <summary>
    /// 固定资产绑定用户 🔖
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "DistributeToUser"), HttpPost]
    [DisplayName("固定资产绑定用户")]
    public async Task FixedAssetDistributeToUser(BindFixedAssetToUserInput input)
    {

    }

    /// <summary>
    /// 固定资产绑定部门 🔖
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "DistributeToOrg"), HttpPost]
    [DisplayName("固定资产绑定部门")]
    public async Task FixedAssetDistributeToOrg(BindFixedAssetToOrgInput input)
    {
    }

    /// <summary>
    /// 固定资产绑定公司 🔖
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "DistributeToCompany"), HttpPost]
    [DisplayName("固定资产绑定公司")]
    public async Task FixedAssetDistributeToCompany(BindFixedAssetToCompanyInput input)
    {
    }

    /// <summary>
    /// 获取固定资产详细信息 🔖
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "Detail"), HttpPost]
    [DisplayName("获取固定资产详细信息")]
    public async Task<FixedAssetOutput> Detail(DetailFixedAssetInput input)
    {
        var _query = await _fixedAssetRepository.AsQueryable()
           .WhereIF(_userManager.SuperAdmin && input.TenantId > 0, u => u.TenantId == input.TenantId)
           .WhereIF(!_userManager.SuperAdmin, u => u.TenantId == _userManager.TenantId) // 若非超管，则只能操作本租户的角色
           .WhereIF(!string.IsNullOrWhiteSpace(input.Code), u => u.Code.Contains(input.Code))
          .FirstAsync();

        return _query.Adapt<FixedAssetOutput>();
    }
}